-- Lua Battle System - 玩家角色

local Character = require("characters.character")
local Logger = require("utils.logger")

local Player = Character:new({
    -- 玩家特有属性
    is_controlled = true, -- 玩家角色由玩家控制
    experience = 0,
    next_level_exp = 100,
    gold = 0,
    
    -- 玩家统计信息
    stats = {
        battles_won = 0,
        battles_lost = 0,
        total_damage_dealt = 0,
        total_damage_taken = 0,
        total_healing_done = 0
    },
    
    -- 玩家技能树
    skill_tree = {},
    
    -- 玩家装备栏扩展
    equipment = {
        weapon = nil,
        armor = nil,
        accessory = nil,
        accessory2 = nil -- 玩家额外的饰品栏
    }
})

-- 玩家构造函数
function Player:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    o:init()
    return o
end

-- 初始化玩家
function Player:init()
    Character.init(self)
    self.experience = 0
    self.next_level_exp = 100
    self.gold = 0
    self.stats = {
        battles_won = 0,
        battles_lost = 0,
        total_damage_dealt = 0,
        total_damage_taken = 0,
        total_healing_done = 0
    }
    self.skill_tree = {}
    self.equipment.accessory2 = nil -- 初始化额外的饰品栏
    
    Logger.debug("玩家角色初始化完成: " .. self.name)
end

-- 获得经验值
function Player:gain_experience(amount)
    self.experience = self.experience + amount
    Logger.info(self.name .. " 获得了 " .. amount .. " 点经验值")
    
    -- 检查是否升级
    while self.experience >= self.next_level_exp do
        self:level_up()
    end
end

-- 升级
function Player:level_up()
    self.level = self.level + 1
    self.experience = self.experience - self.next_level_exp
    
    -- 计算下一级所需经验
    self.next_level_exp = math.floor(self.next_level_exp * 1.5)
    
    -- 属性成长
    local growth_rate = 0.1 -- 成长率
    self.max_hp = math.floor(self.max_hp * (1 + growth_rate))
    self.hp = self.max_hp -- 升级时回满生命值
    self.max_mp = math.floor(self.max_mp * (1 + growth_rate))
    self.mp = self.max_mp -- 升级时回满魔法值
    self.attack = math.floor(self.attack * (1 + growth_rate))
    self.defense = math.floor(self.defense * (1 + growth_rate))
    self.magic_attack = math.floor(self.magic_attack * (1 + growth_rate))
    self.magic_defense = math.floor(self.magic_defense * (1 + growth_rate))
    self.speed = math.floor(self.speed * (1 + growth_rate))
    self.luck = math.floor(self.luck * (1 + growth_rate))
    
    Logger.info(self.name .. " 升级了！现在是 " .. self.level .. " 级")
    Logger.info("生命值: " .. self.max_hp .. ", 魔法值: " .. self.max_mp)
    Logger.info("攻击: " .. self.attack .. ", 防御: " .. self.defense)
    Logger.info("魔法攻击: " .. self.magic_attack .. ", 魔法防御: " .. self.magic_defense)
    Logger.info("速度: " .. self.speed .. ", 幸运: " .. self.luck)
end

-- 获得金币
function Player:gain_gold(amount)
    self.gold = self.gold + amount
    Logger.info(self.name .. " 获得了 " .. amount .. " 金币")
end

-- 花费金币
function Player:spend_gold(amount)
    if self.gold < amount then
        Logger.warn(self.name .. " 金币不足，当前金币: " .. self.gold .. "，需要: " .. amount)
        return false
    end
    
    self.gold = self.gold - amount
    Logger.info(self.name .. " 花费了 " .. amount .. " 金币")
    return true
end

-- 更新战斗统计信息
function Player:update_battle_stats(stats)
    if stats.damage_dealt then
        self.stats.total_damage_dealt = self.stats.total_damage_dealt + stats.damage_dealt
    end
    
    if stats.damage_taken then
        self.stats.total_damage_taken = self.stats.total_damage_taken + stats.damage_taken
    end
    
    if stats.healing_done then
        self.stats.total_healing_done = self.stats.total_healing_done + stats.healing_done
    end
end

-- 战斗胜利
function Player:on_battle_win()
    self.stats.battles_won = self.stats.battles_won + 1
    Logger.info(self.name .. " 战斗胜利！总胜利场次: " .. self.stats.battles_won)
end

-- 战斗失败
function Player:on_battle_lost()
    self.stats.battles_lost = self.stats.battles_lost + 1
    Logger.info(self.name .. " 战斗失败！总失败场次: " .. self.stats.battles_lost)
end

-- 学习技能
function Player:learn_skill(skill, skill_tree_path)
    -- 添加技能到技能列表
    self:add_skill(skill)
    
    -- 更新技能树
    if skill_tree_path then
        local current_node = self.skill_tree
        for _, node_name in ipairs(skill_tree_path) do
            if not current_node[node_name] then
                current_node[node_name] = {}
            end
            current_node = current_node[node_name]
        end
        current_node[skill.id] = true
    end
    
    Logger.info(self.name .. " 学会了新技能: " .. skill.name)
end

-- 装备物品（扩展支持第二个饰品栏）
function Player:equip_item(item)
    if item.type ~= "equipment" then
        Logger.warn(item.name .. " 不是装备物品")
        return false
    end
    
    local slot = item.equipment_slot
    if not slot then
        Logger.warn(item.name .. " 没有指定装备槽位")
        return false
    end
    
    -- 处理第二个饰品栏
    if slot == "accessory" then
        if not self.equipment.accessory then
            slot = "accessory"
        else
            slot = "accessory2"
        end
    end
    
    -- 卸下当前装备
    local current_equipment = self.equipment[slot]
    if current_equipment then
        self:unequip_item(slot)
    end
    
    -- 装备新物品
    self.equipment[slot] = item
    item:on_equip(self)
    Logger.info(self.name .. " 装备了 " .. item.name .. " 到 " .. slot .. " 槽位")
    return true
end

-- 获取玩家状态（扩展）
function Player:get_state()
    local state = Character.get_state(self)
    state.experience = self.experience
    state.next_level_exp = self.next_level_exp
    state.gold = self.gold
    state.stats = self.stats
    return state
end

-- 导出模块
return Player